use std::path::Path;
use std::time::Duration;
use notify::{RecursiveMode, Watcher};
use notify_debouncer_full::new_debouncer;

fn _watch_folder_changes() {
    let path = "/Users/wangbin/Downloads/test-folder";
    log::info!("Watching {path}");
    if let Err(error) = _watch_debounce(path) {
        log::error!("Error: {error:?}");
    }
}

fn _watch_debounce<P: AsRef<Path>>(path: P) -> notify::Result<()> {
    let (tx, rx) = std::sync::mpsc::channel();
    let mut debouncer = new_debouncer(Duration::from_secs(2), None, tx)?;
    debouncer.watcher().watch(path.as_ref(), RecursiveMode::Recursive)?;
    for result in rx {
        match result {
            Ok(events) => events.iter().for_each(|event| log::debug!("{event:?}")),
            Err(errors) => errors.iter().for_each(|error| log::error!("{error:?}")),
        }
    }
    Ok(())
}
